(function() {
	'use strict';
	
	var unitVue = new Vue({
		el: '#main',
		store: store,
		data: function() {
			return {
				stepActive: 0,
				panelTitles: ['基础配置', '编排', '发布']
			};
		},
		methods: {
			pre: function() {
				this.stepActive != 0 ? this.stepActive-- : this.stepActive;
			},
            publish: function () {
                var self = this;
                this.$refs['baseForm'].$refs['baseForm'].validate(function (valid) {
                    var unit = _.extend({}, self.$store.state.unit);
                    axios.get(_contextPath+'/open/datastash/unit/publish/'+unit.id )
                        .then(function (response) {
                            if(response.data.success){
                                self.getData(unit.id);
                                unitVue.$alert('发布成功！', '提示', {
                                    confirmButtonText: '确定'
                                });
                            }else{
                                unitVue.$alert('发布成功！错误信息：'+response.data.msg, '提示', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                        .catch(function (error) {
                            unitVue.$alert('保存失败！', '提示', {
                                confirmButtonText: '确定'
                            });
                        });
                })
            },
			submit: function() {
				var self = this;
				this.$refs['baseForm'].$refs['baseForm'].validate(function (valid) {
					self.$store.commit('getStepData');
					var unit = _.extend({}, self.$store.state.unit);
                    axios.post(_contextPath+'/open/datastash/unit/creator',unit )
                        .then(function (response) {
                        	if(response.data.success){
                                unitVue.$alert('保存成功！', '提示', {
                                    confirmButtonText: '确定'
                                });
							}else{
                                unitVue.$alert('保存失败！错误信息：'+response.data.msg, '提示', {
                                    confirmButtonText: '确定'
                                });
							}
                        })
                        .catch(function (error) {
                            unitVue.$alert('保存失败！', '提示', {
                                confirmButtonText: '确定'
                            });
                        });
				})
			},
			next: function() {
				var self = this;
				if (this.stepActive == 0) {
					this.$refs['baseForm'].$refs['baseForm'].validate(function (valid) {
						if (valid) {
							self.stepActive++;
						}
					});
				} else {
					this.stepActive != 2 ? this.stepActive++ : this.stepActive;
				}
			},
			getData:function (dataId) {
                var self = this;
                axios.get(_contextPath + '/open/datastash/unit/' + dataId)
                    .then(function (response) {
                        if (response.data) {
                            _self.$store.commit('editUnit', response.data);
                            _self.$store.commit('renderGraphXml', response.data.graph);
                        } else {
                            unitVue.$alert('查询数据为空！', '提示', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                    .catch(function (error) {
                    });
            }

		},
		watch: {
			'stepActive': function(val, oldVal) {
				if (val==1 && editorUi) {
					this.$nextTick(function() {
						editorUi.refresh();
					})
				}
			}
		},
        computed: {
			'state':function() {
                return this.$store.state.unit.state;
            }
        }
		,
		created: function() {
            var _self = this;
			if (this.$store.state.oper == 'edit') {
                axios.get(_contextPath + '/open/datastash/unit/' + dataId)
                    .then(function (response) {
                        if (response.data) {
                            _self.$store.commit('editUnit', response.data);
                            _self.$store.commit('renderGraphXml', response.data.graph);
                        } else {
                            unitVue.$alert('查询数据为空！', '提示', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                    .catch(function (error) {
                    });
			}
		},
		mounted: function() {
        }
	})
}())